package practice.book

import akka.actor.{Actor, ActorRef, ActorSystem}
import akka.actor.Actor.Receive
import practice.book.LearningAkka.Chapter1.{AkkademyDb, SetRequest}

/**
  * Created by lovepocky(lovepocky@foxmail.com) on 2016/4/13.
  */
object LearningAkka {
	def main(args: Array[String]) {
		new Chapter1
	}
	
	
	object Chapter1 {
		
		case class SetRequest(key: String, value: Object)
		
		class AkkademyDb extends Actor {
			
			import scala.collection.mutable.HashMap
			import akka.event.Logging
			
			val map = new HashMap[String, Object]
			val log = Logging(context.system, this)
			
			override def receive: Receive = {
				case SetRequest(key, value) =>
					log.info(s"received SetRequest - key: $key value: $value")
					map.put(key, value)
				case unknown => log.info("unknow message: {}", unknown)
			}
		}
		
	}
	
	class Chapter1 extends {
		println("Chapter1")
		
		import akka.actor.Props
		
		implicit val system = ActorSystem("Chapter1-Akka-System")
		val actor = system.actorOf(Props[AkkademyDb])
		actor ! SetRequest("key", "hello")
		
		println("End Of Chapter1")
	}
	
}
